package com.xxljob.demo.service;

import com.xxl.job.core.biz.model.HandleCallbackParam;
import com.xxl.job.core.executor.XxlJobExecutor;
import com.xxljob.demo.dao.XxlJobInfoDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Author：YH
 * @Description：测试实现类没用
 * @Date:Created in 2022/5/24 17:16
 */
@Service
public class TriggerCallbackThread {

    private static final Logger logger = LoggerFactory.getLogger(TriggerCallbackThread.class);

    public TriggerCallbackThread() {
    }


    public static TriggerCallbackThread getInstance() {
        return instance;
    }

    private static final TriggerCallbackThread instance = new TriggerCallbackThread();
    private final LinkedBlockingQueue<HandleCallbackParam> callBackQueue = new LinkedBlockingQueue();
    private Thread triggerCallbackThread;
    private Thread triggerRetryCallbackThread;
    private final boolean toStop = false;


    @Resource
    private XxlJobInfoDao xxlJobInfoDao;

    public void start() {
        if (XxlJobExecutor.getAdminBizList() != null) {
            logger.warn(">>>>>>>>>>> xxl-job, executor callback config fail, adminAddresses is null.");
        } else {
            this.triggerCallbackThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (!TriggerCallbackThread.this.toStop) {
                        try {
                            logger.info("triggerCallbackThread运行");
                            HandleCallbackParam callback =
                                    TriggerCallbackThread.getInstance().callBackQueue.take();
                            if (callback != null) {
                                List<HandleCallbackParam> callbackParamListx = new ArrayList();
                                int drainToNumx =
                                        TriggerCallbackThread.getInstance().callBackQueue.drainTo(callbackParamListx);
                                callbackParamListx.add(callback);
                                if (callbackParamListx != null && callbackParamListx.size() > 0) {
                                    //TriggerCallbackThread.this.doCallback(callbackParamListx);
                                }
                            }
                        } catch (Exception var4) {
                            if (!TriggerCallbackThread.this.toStop) {
                                TriggerCallbackThread.logger.error(var4.getMessage(), var4);
                            }
                        }
                    }
                    logger.info("运行到下面了");

                    try {
                        List<HandleCallbackParam> callbackParamList = new ArrayList();
                        int drainToNum = TriggerCallbackThread.getInstance().callBackQueue.drainTo(callbackParamList);
                        if (callbackParamList != null && callbackParamList.size() > 0) {
                            //TriggerCallbackThread.this.doCallback(callbackParamList);
                        }
                    } catch (Exception var5) {
                        if (!TriggerCallbackThread.this.toStop) {
                            logger.error(var5.getMessage(), var5);
                        }
                    }
                    logger.info(">>>>>>>>>>> xxl-job, executor callback" +
                            " thread destory.");
                }
            });
            this.triggerCallbackThread.setDaemon(true);
            this.triggerCallbackThread.setName("triggerCallbackThread");
            this.triggerCallbackThread.start();
        }
    }
}
